﻿package fileInterface
{
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2World;
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import fileInterface.MenuButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import SWFKit.File;
	import SWFKit.Shell;
	
	/**
	 * ...
	 * @author GertS
	 */
	public class FileInterface extends Sprite
	{
		
		private var _buttonList:/*MenuButton*/Array;
		//private var _radius:Number = 70;
		public var body:b2Body;
		public var bodySprite:Sprite;
		private var _prevBody:b2Body;
		public static var box2DHolder:Sprite;
		public static var world:b2World;
		private var _tooltip_txt:TextField;
		
		public function FileInterface() 
		{
			super();
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler)
			//x = stage.stageWidth / 2;
			//y = stage.stageHeight / 2;
			
			initTooltip()
		}
		
		private function initTooltip():void
		{
			_tooltip_txt = new TextField();
			addChild(_tooltip_txt);
			//mouseChildren = false;
			_tooltip_txt.mouseEnabled = false;
			_tooltip_txt.selectable = false;
			_tooltip_txt.autoSize = TextFieldAutoSize.CENTER;
			var tf:TextFormat = new TextFormat("Tahoma", 15, 0xeeeeee, true);
			_tooltip_txt.setTextFormat(tf);
			_tooltip_txt.defaultTextFormat = tf;
			_tooltip_txt.visible = false;
			
			var dropShadow:DropShadowFilter = new DropShadowFilter();
			dropShadow.distance = 0;
			dropShadow.angle = 45;
			dropShadow.color = 0x333333;
			dropShadow.alpha = 1;
			dropShadow.blurX = 5;
			dropShadow.blurY = 5;
			dropShadow.strength = 1;
			dropShadow.inner = false;
			dropShadow.knockout = false;
			dropShadow.hideObject = false;
			dropShadow.quality =  BitmapFilterQuality.LOW;
			_tooltip_txt.filters = new Array(dropShadow);
		}
		
		public function showTooltip(x, y:Number, txt:String) {
			setChildIndex(_tooltip_txt, numChildren - 1);
			_tooltip_txt.text = txt;
			_tooltip_txt.y = y - 32;
			_tooltip_txt.x = x - (_tooltip_txt.width / 2);
			TweenLite.to(_tooltip_txt, 0.4, { y: y-42, autoAlpha:1 } );
		}
		
		public function hideTooltip() {
			TweenLite.to(_tooltip_txt, 0.4, { y: _tooltip_txt.y + 10, autoAlpha:0 } );
		}
		
		private function addedToStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			parent.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);			
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			hide();
		}
		
		private function enterFrameHandler(e:Event):void 
		{
			if (body && visible) {
				x += (bodySprite.x - x) / 10;
				y += (bodySprite.y - y) / 10;
			}
		}
		
		public function set attachedBody(body_:b2Body):void {
			
			body = body_;
			bodySprite = body.GetUserData() as Sprite;
			show();
		}
		
		private function onOpenFolderClick(e:MouseEvent):void
		{
			new Shell().run("explorer.exe " + SnippingTool(parent).path)
		}
		
		public function  get fileName():String {
			return SnippingTool.imageList[bodySprite.name.split("img").join("")];
		}
		
		public function  get folderPath():String {
			return SnippingTool(parent).path;
		}
		
		
		public function show():void
		{
			if (x == 0 && y == 0) {
				x = bodySprite.x;
				y = bodySprite.y;
			}
			var spreadAngle:Number = (Math.PI * 0.7);
			var deltaA:Number = spreadAngle / (_buttonList.length - 1);
			var startAngle =  - (spreadAngle / 2);
			visible = true;
			mouseEnabled = true;
			for (var i:int = 0; i < _buttonList.length; i++) {
				if (!_buttonList[i].parent) {
					addChild(_buttonList[i]);
				}
				_buttonList[i].alpha = 0;
				//_buttonList[i].x = 0;
				//_buttonList[i].y = 0;
				
				var radius:Number = Math.max(40, (bodySprite.width + bodySprite.height) / 2.7);
				
				_buttonList[i].holderRotation = startAngle + (i * deltaA)
				var newX:Number = Math.sin(startAngle + (i * deltaA)) * radius;
				var newY:Number = -Math.cos(startAngle + (i * deltaA)) * radius;
				TweenLite.to(_buttonList[i], 0.4, {x:newX, y:newY, alpha:1} );
			}
		}
		
		public function set settingsXML(value:XML):void
		{
			_buttonList = new Array();
			for each(var xmlChild:XML in value.externalActions.action) {
				_buttonList.push(new MenuButton(this, xmlChild.icon, xmlChild.tooltip, xmlChild.externalCommand, xmlChild.internalCommand));
			}
		}
		
		public function hide():void	{
			for (var i:int = 0; i < _buttonList.length; i++) {
				TweenLite.to(_buttonList[i], 0.4, {x:0, y:0, alpha:0, onComplete:makeUnvisible} );
			}
		}
		
		private function makeUnvisible():void
		{
			visible = false;
			mouseEnabled = false;
			x = 0;
			y = 0;
		}
		
	}

}